package org.jeecg.modules.compilation.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.MinioUtil;
import org.jeecg.config.oss.MinioConfig;
import org.jeecg.modules.compilation.dto.CiBusinessCategoryDTO;
import org.jeecg.modules.compilation.entity.CiBusinessCategory;
import org.jeecg.modules.compilation.service.ICiBusinessCategoryService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.compilation.service.MinioService;
import org.jeecg.modules.compilation.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 业务类型管理
 * @Author: jeecg-boot
 * @Date:   2024-03-15
 * @Version: V1.0
 */
@Api(tags="业务类型管理")
@RestController
@RequestMapping("/compilation/ciBusinessCategory")
@Slf4j
public class CiBusinessCategoryController extends JeecgController<CiBusinessCategory, ICiBusinessCategoryService> {
	@Autowired
	private ICiBusinessCategoryService ciBusinessCategoryService;
	@Autowired
	private MinioService minioService;

	@Autowired
	private MinioConfig config;
	 /**
	  *
	  */
	 /**
	  * 下拉框
	  */
	 @AutoLog(value = "下拉框")
	 @ApiOperation(value="下拉框", notes="下拉框")
	 @GetMapping("/queryList")
	 public Result<List<CiBusinessCategoryDTO>> queryList(){
		 return Result.OK( ciBusinessCategoryService.queryList());
	 }







	/**--------------------------------------------------------------------------------------------------------*/

	/**
	 * 分页列表查询
	 *
	 * @param ciBusinessCategory
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "业务类型管理-分页列表查询")
	@ApiOperation(value="业务类型管理-分页列表查询", notes="业务类型管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CiBusinessCategory>> queryPageList(CiBusinessCategory ciBusinessCategory,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CiBusinessCategory> queryWrapper = QueryGenerator.initQueryWrapper(ciBusinessCategory, req.getParameterMap());
		Page<CiBusinessCategory> page = new Page<CiBusinessCategory>(pageNo, pageSize);
		IPage<CiBusinessCategory> pageList = ciBusinessCategoryService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param ciBusinessCategory
	 * @return
	 */
	@AutoLog(value = "业务类型管理-添加")
	@ApiOperation(value="业务类型管理-添加", notes="业务类型管理-添加")
//	@RequiresPermissions("compilation:ci_business_category:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CiBusinessCategory ciBusinessCategory) {
		ciBusinessCategoryService.save(ciBusinessCategory);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param ciBusinessCategory
	 * @return
	 */
	@AutoLog(value = "业务类型管理-编辑")
	@ApiOperation(value="业务类型管理-编辑", notes="业务类型管理-编辑")
//	@RequiresPermissions("compilation:ci_business_category:edit")
	@RequestMapping(value = "/addOrEditIcon", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestPart("dto") CiBusinessCategory ciBusinessCategory,
							   @RequestPart(value = "img",required = false) MultipartFile image) {
		if(StringUtils.isEmpty(ciBusinessCategory.getBusinessCategorySn())){
			ciBusinessCategory.setBusinessCategorySn(CommonUtil.getSn());
		}
		if(image!=null) {
			String url = minioService.uploadFile(image, "compilation-institutions", null);
			ciBusinessCategory.setBusinessCategoryUrl(url);
			ciBusinessCategory.setRealPath(url);
		}
		ciBusinessCategoryService.saveOrUpdate(ciBusinessCategory);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "业务类型管理-通过id删除")
	@ApiOperation(value="业务类型管理-通过id删除", notes="业务类型管理-通过id删除")
//	@RequiresPermissions("compilation:ci_business_category:delete")
	@GetMapping(value = "/removeBySn")
	public Result<String> delete(@RequestParam(name="businessCategorySn",required=true) String businessCategorySn) {
		LambdaQueryWrapper<CiBusinessCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(CiBusinessCategory::getBusinessCategorySn,businessCategorySn);
		ciBusinessCategoryService.remove(lambdaQueryWrapper);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "业务类型管理-批量删除")
	@ApiOperation(value="业务类型管理-批量删除", notes="业务类型管理-批量删除")
//	@RequiresPermissions("compilation:ci_business_category:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.ciBusinessCategoryService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "业务类型管理-通过id查询")
	@ApiOperation(value="业务类型管理-通过id查询", notes="业务类型管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CiBusinessCategory> queryById(@RequestParam(name="id",required=true) String id) {
		CiBusinessCategory ciBusinessCategory = ciBusinessCategoryService.getById(id);
		if(ciBusinessCategory==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ciBusinessCategory);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ciBusinessCategory
    */
//    @RequiresPermissions("compilation:ci_business_category:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CiBusinessCategory ciBusinessCategory) {
        return super.exportXls(request, ciBusinessCategory, CiBusinessCategory.class, "业务类型管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("compilation:ci_business_category:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CiBusinessCategory.class);
    }

}
